package com.kkb.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Team;
import com.kkb.service.TeamService;
import com.kkb.vo.PageParam;
import com.kkb.vo.ResultVO;
import com.kkb.vo.TeamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 球队 Controller
 *
 * @author ztranscript 2021/8/30 16:06
 **/
@RestController
@RequestMapping("/team")
public class TeamController {
    @Autowired
    private TeamService teamService;

    /**
     * 分页查询球队信息
     *
     * @param teamVO    查询条件
     * @param pageParam 分页条件
     * @return 球队信息
     */
    @GetMapping("/list")
    public ResultVO<Team> list(TeamVO teamVO, PageParam pageParam) {
        PageInfo<Team> pageInfo = teamService.queryByPage(teamVO, pageParam);
        return new ResultVO<>(pageInfo);
    }

    /**
     * 增加球队
     *
     * @param team 球队信息
     * @return 响应
     */
    @PostMapping("")
    public ResultVO<Team> addTeam(Team team) {
        int rows = teamService.addTeam(team);
        if (rows == 1) {
            return new ResultVO<Team>();
        }
        return new ResultVO<>(500, "服务器内部异常，请稍后再试！");
    }

    /**
     * 根据主键查询
     *
     * @param teamId 球队 ID
     * @return 响应
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO<Team> queryById(@PathVariable("id") Integer teamId) {
        Team team = teamService.queryById(teamId);
        return new ResultVO<>(team);
    }

    /**
     * 根据主键更新
     *
     * @param teamId 球队 ID
     * @param team   更新信息
     * @return 响应
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResultVO<Team> updateTeam(@PathVariable("id") Integer teamId, Team team) {
        System.out.println("updateTeam--team-------------" + team);
        team.setTeamId(teamId);
        int rows = teamService.updateTeam(team);
        if (rows == 1) {
            return new ResultVO<Team>();
        }
        return new ResultVO<>(500, "服务器内部异常，请稍后再试！");
    }

    /**
     * 根据主键删除
     *
     * @param teamId 主键 ID
     * @return 响应
     */
    @DeleteMapping("/{id}")
    public ResultVO<Team> deleteTeam(@PathVariable("id") Integer teamId) {
        int rows = teamService.deleteTeam(teamId);
        if (rows == 1) {
            return new ResultVO<Team>();
        }
        return new ResultVO<>(500, "服务器内部异常，请稍后再试！");
    }

    @DeleteMapping("")
    public ResultVO<Team> batchDelete(List<Integer> teamIdList) {
        if (CollectionUtils.isEmpty(teamIdList)) {
            return new ResultVO<>();
        }
        int rows = teamService.batchDelete(teamIdList);
        if (rows == teamIdList.size()) {
            return new ResultVO<>();
        }
        return new ResultVO<>(500, "服务器内部异常，请稍后再试！");
    }

    /**
     * 文件上传(logo)
     *
     * @param myFile  logo
     * @param teamId  球队 ID
     * @param request request
     * @return 响应
     */
    @PostMapping("{id}")
    public ResultVO<Team> uploadLogo(@RequestParam("logo") MultipartFile myFile,
                                     @PathVariable("id") Integer teamId,
                                     HttpServletRequest request) {
        // 文件上传到指定服务器位置
        String path = request.getServletContext().getRealPath("/img/uploadFile/");

        // 获取原始文件的名称
        String originalFilename = myFile.getOriginalFilename();

        if (Objects.isNull(originalFilename)) {
            return new ResultVO<>(500, "图片上传出现问题！");
        }

        // 定义文件的新名词：随机生成的名称+原有文件的后缀名
        String randomName = UUID.randomUUID().toString().replace("-", "");
        int index = originalFilename.lastIndexOf(".");
        String hz = originalFilename.substring(index);
        String logoName = randomName + hz;

        int num = 0;
        try {
            myFile.transferTo(new File(path + "/" + logoName));
            System.out.println("上传成功！" + path + "/" + logoName);

            //将图片的名称更新到数据库中
            Team team = new Team();
            team.setTeamId(teamId);
            team.setTeamLogo(logoName);
            num = teamService.updateTeam(team);
            if (num == 1) {
                return new ResultVO<Team>();
            } else {
                return new ResultVO<>(500, "服务器内部异常，请稍后再试！");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new ResultVO<>(500, "图片上传出现问题！" + e.getMessage());
        }
    }
}
